import sys

import dal
from dl_phone import *
from PyQt5.Qt import *
from tableModel import *


class frm_phone(QDialog, Ui_frm_dlg_phone):
    data_dic = {}
    data_list = []
    phone_dic = {}
    phone_list = {}
    phone = None

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.br_listview()
        self.btn_Add.clicked.connect(self.phone_add)
        self.list_br.clicked.connect(self.list_clicked)
        self.list_phone.clicked.connect(self.list_phone_clicked)
        self.btn_Del.clicked.connect(self.phone_del)

    my_Signal = QtCore.pyqtSignal(str)

    def sendEditContent(self):
        content = '2'
        self.my_Signal.emit(content)

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        self.sendEditContent()

    # 刷新列表框
    def ref_listview(self, listName, listDate):
        slm = slm = QStringListModel()
        slm.setStringList(listDate)
        listName.setModel(slm)

    # 填充列表框
    def br_listview(self):
        global data_list
        global data_dic
        data_dic, data_list = dal.date_all(Br, 'brID', 'brName')
        self.ref_listview(self.list_br, data_list)

    # 添加事件
    def phone_add(self):
        global phone
        global phone_dic
        global phone_list

        if self.list_br.currentIndex().row() == -1:
            QMessageBox.critical(self, "请检查", "请选择品牌", QMessageBox.Yes)
            return
        phone.phName = self.txt_phone.text().upper()
        # print(phone.phName)
        Sess = sessionmaker(bind=engine)
        ss = Sess()
        # print("------"+str(dal.checks(Br, "亿")))
        filterStr = self.txt_phone.text().upper()
        user = ss.query(Phone).filter(eval('Phone.phName=="{}"'.format(filterStr)),Phone.brID=="{}".format(phone.brID)).all()
        # user = ss.query(Phone).filter(Phone.phName=="R9").all()
        print(user)
        # if self.list_br.currentIndex().row() == -1:



        if len(user) == 0 and len(self.txt_phone.text()) != 0:
            print("ok")
            ss.add(phone)
            ss.commit()
            str = phone.brID
            str_filter = "Phone.brID=='{}'".format(str)
            print(str_filter)
            phone_dic, phone_list = dal.date_select(Phone, 'id', 'phName', str_filter)
            self.ref_listview(self.list_phone, phone_list)
            self.txt_phone.setText('')
            phone = Phone()
            phone.brID=str

        else:
            QMessageBox.critical(self, "请检查", "手机机型没有输入或已经存在", QMessageBox.Yes)
            return

    # 品牌列表左键单击
    def list_clicked(self, index):

        # self.txt_br.setText(self.list_br.selectionModel().selectedIndexes()[0].data())
        global data_dic
        global data_list
        global phone_dic
        global phone_list
        global phone
        phone = Phone()

        temp_dic = {}
        for k, v in data_dic.items():
            temp_dic[v] = k

        str_br = data_list[index.row()]
        str_id = str(temp_dic[str_br])
        phone.brID = int(str_id)
        # print(str_id)
        str_filter = "Phone.brID==" + str_id
        # print(str_filter)
        phone_dic, phone_list = dal.date_select(Phone, 'id', 'phName', str_filter)
        self.ref_listview(self.list_phone, phone_list)

    # 机型列表左键单击
    def list_phone_clicked(self):

        # self.txt_phone.text(self.list_phone.selectionModel().selectedIndexes()[0].data())
        # self.txt_phone.text("ok")
        tempStr = self.list_phone.selectionModel().selectedIndexes()[0].data()
        self.txt_phone.setText(tempStr)



    # 删除按钮左键单击
    def phone_del(self):
        global phone
        global phone_dic
        # print(phone)
        if self.list_phone.currentIndex().row() == -1:
            QMessageBox.critical(self, "请检查", "请选择机型", QMessageBox.Yes)
            return

        temp_dic = {}
        for k, v in phone_dic.items():
            temp_dic[v] = k
        # print(temp_dic)
        str_phone_id = temp_dic[self.txt_phone.text()]
        phone.id = str_phone_id
        # print(phone.id)
        # self.check_none()
        if self.check_none():
            QMessageBox.critical(self, "请检查", "此机型下有维修单，不可删除！", QMessageBox.Yes)
        else:
            # global phone
            Sess = sessionmaker(bind=engine)
            ss = Sess()
            # str = self.list_phone.selectionModel().selectedIndexes()[0].data()
            phone.phName=self.txt_phone.text()
            filterStr= "Phone.id=='{}'".format(phone.id)
            print(filterStr)
        #     filterStr = 'Phone.phName=="{}"'.format(self.txt_br.text())
        #     filterStrId = 'Phone.phID=="{}"'.format(filterStr)
            # print(filterStr)
            result = ss.query(Phone).filter(eval(filterStr)).first()
            print(result)
            # print('手机机型ID是' + phone.id)
            ss.delete(result)
            ss.commit()
            self.txt_phone.setText('')
            # self.list_clicked()


            # global data_dic
            # global data_list
            # # global phone_dic
            # global phone_list
            # # global phone
            # phone = Phone()

            # temp_dic = {}
            # for k, v in data_dic.items():
            #     temp_dic[v] = k

            # str_br = data_list[index.row()]
            # str_id = str(temp_dic[str_br])
            # phone.brID = int(str_id)
            # print(str_id)
            print(phone.brID)
            str_filter = "Phone.brID=='{}'".format(phone.brID)
            print(str_filter)
            phone_dic, phone_list = dal.date_select(Phone, 'id', 'phName', str_filter)
            print(phone_list)
            self.ref_listview(self.list_phone, phone_list)

    def check_none(self):

        global phone

        Sess = sessionmaker(bind=engine)
        ss = Sess()
        filterStr = 'PhEdit.phID=="{}"'.format(phone.id)
        result = ss.query(PhEdit).filter(eval(filterStr)).all()
        # filterStr = 'Phone.brID=="{}"'.format(str)
        # print(filterStr)
        # result = ss.query(Phone).filter(Phone.brID=='2').all()
        # result = ss.query(Phone).filter(eval(filterStr)).all()
        # phone.phName=result.phName
        # print(result.phName)
        # print(result)
        if len(result) == 0:
            return False
        else:
            return True


if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)

    win = frm_phone()
    win.show()

    sys.exit(app.exec_())
